﻿using System;
using System.Windows.Forms;
using Sumen.Business;
using Sumen.Business.Presenter.Actor;
using DevExpress.XtraEditors;
using Sumen.Business.Presenter.Category;
using Sumen.Business.Presenter.Sys;
using Sumen.Library.Classes.Common;
using Sumen.Library.Enum;
using Sumen.Model.Model;

namespace Sumen.GUI.Entities
{
    public partial class Frm_Customer_Edit : XtraForm
    {
        #region Presenter

        private CustomerPresenter _customerPresenter;
        private CustomerTypePresenter _customerTypePresenter;

        #endregion

        #region Property
        private customer _customer;
        private long sorted;
        private EnumFormCode _fromCode = EnumFormCode.FrmCustomer;
        public customer Customer
        {
            get { return _customer; }
            set { _customer = value; }
        }

        private EnumFormStatus _enumFormStatus;
        public EnumFormStatus EnumFormStatus
        {
            get { return _enumFormStatus; }
            set
            {
                _enumFormStatus = value;
                if (_enumFormStatus == EnumFormStatus.Add)
                {
                    buttonsArray1.btnSua.Enabled = false;
                    buttonsArray1.btnThem.Enabled = true;
                }
                else if (_enumFormStatus == EnumFormStatus.Modify)
                {
                    buttonsArray1.btnSua.Enabled = true;
                    buttonsArray1.btnThem.Enabled = false;
                }
            }
        }
        #endregion

        public Frm_Customer_Edit()
        {
            InitializeComponent();
            _customerTypePresenter = new CustomerTypePresenter();
            _customerPresenter = new CustomerPresenter();
        }

        private void Frm_Customer_Edit_Load(object sender, EventArgs e)
        {
            lookUpCusType.Properties.DataSource = _customerTypePresenter.GetAll();
            LoadForm();
        }

        #region Method

        void LoadForm()
        {
            if (_enumFormStatus == EnumFormStatus.Add)
            {
                txtUsername.EditValue = null;
                txtUsername.Properties.ReadOnly = false;
                txtPass.EditValue = null;
                lookUpCusType.EditValue = null;
                txtFullName.EditValue = null;
                txtPhone.EditValue = null;
                txtAddress.EditValue = null;
                txtDistrict.EditValue = null;
                txtCity.EditValue = null;
                txtContact.EditValue = null;
                txtEmail.EditValue = null;
                txtFacebok.EditValue = null;
                chkActived.CheckState = CheckState.Checked;
            }
            else if (_enumFormStatus == EnumFormStatus.Modify)
            {
                txtPrefix.EditValue = _customer.Cus_Username.Substring(0, 2);
                txtUsername.EditValue = _customer.Cus_Username.Substring(2, _customer.Cus_Username.Length - 2);
                txtUsername.Properties.ReadOnly = true;
                txtPass.EditValue = null;
                lookUpCusType.EditValue = _customer.Cus_CUSTOMER_TYPE_ID;
                txtFullName.EditValue = _customer.Cus_FullName;
                txtPhone.EditValue = _customer.Cus_Phone;
                txtAddress.EditValue = _customer.Cus_Address;
                txtDistrict.EditValue = _customer.Cus_District;
                txtCity.EditValue = _customer.Cus_City;
                txtContact.EditValue = _customer.Cus_Contact;
                txtEmail.EditValue = _customer.Cus_Email;
                txtFacebok.EditValue = _customer.Cus_Facebook;
                chkActived.CheckState = _customer.Cus_Actived == 1 ? CheckState.Checked : CheckState.Unchecked;
            }
        }

        bool Validation()
        {
            dxErrorProvider1.ClearErrors();
            if (lookUpCusType.EditValue == null || lookUpCusType.EditValue == "")
            {
                dxErrorProvider1.SetError(lookUpCusType, "Vui lòng chọn loại khách hàng");
                return false;
            }
            else if (txtUsername.EditValue == null || txtUsername.EditValue == "")
            {
                dxErrorProvider1.SetError(txtUsername, "Vui lòng nhập tài khoản");
                return false;
            }
            else if (_enumFormStatus == EnumFormStatus.Add)
            {
                if (txtPass.EditValue == null || txtPass.EditValue == "")
                {
                    dxErrorProvider1.SetError(txtPass, "Vui lòng nhập mật khẩu");
                    return false;
                }
            }
            else if (txtFullName.EditValue == null || txtFullName.EditValue == "")
            {
                dxErrorProvider1.SetError(txtFullName, "Vui lòng nhập họ tên");
                return false;
            }
            else if (txtPhone.EditValue == null || txtPhone.EditValue == "")
            {
                dxErrorProvider1.SetError(txtPhone, "Vui lòng nhập họ tên");
                return false;
            }
            return true;
        }

        customer addCustomer()
        {

            customer newCustomer = new customer();

            newCustomer.Cus_Username = txtPrefix.EditValue + string.Empty + txtUsername.EditValue;
            newCustomer.Cus_Password = txtPass.EditValue + string.Empty;
            newCustomer.Cus_CUSTOMER_TYPE_ID = Convert.ToInt32(lookUpCusType.EditValue);
            newCustomer.Cus_FullName = txtFullName.EditValue + string.Empty;
            newCustomer.Cus_Phone = txtPhone.EditValue + string.Empty;
            newCustomer.Cus_Address = txtAddress.EditValue + string.Empty;
            newCustomer.Cus_District = txtDistrict.EditValue + string.Empty;
            newCustomer.Cus_City = txtCity.EditValue + string.Empty;
            newCustomer.Cus_Contact = txtContact.EditValue + string.Empty;
            newCustomer.Cus_Email = txtEmail.EditValue + string.Empty;
            newCustomer.Cus_Actived = chkActived.CheckState == CheckState.Checked ? 1 : 0;
            newCustomer.Cus_Facebook = txtFacebok.EditValue + string.Empty;
            newCustomer.Cus_Sorted = sorted; return newCustomer;
        }

        #endregion


        #region Button Event

        private void buttonsArray1_btnEventAdd_click(object sender, EventArgs e)
        {
            if (Validation())
            {
                if (_enumFormStatus == EnumFormStatus.Add)
                {
                    //add
                    customer cus = addCustomer();
                    if (_customerPresenter.Add(cus) > 0)
                    {
                        ClsMsgBox.ThongTin("Thêm mới khách hàng thành công");
                        if (ClsPublic.SYSTEM_WRITELOG == "1")
                        {
                            SysLogPresenter log = new SysLogPresenter();
                            log.Add(_fromCode, EnumFormStatus.Add, cus.Cus_Username, "");
                        }
                        _customer = cus;
                        this.EnumFormStatus = EnumFormStatus.Modify;
                    }
                }
            }
        }

        private void buttonsArray1_btnEventUpdate_click(object sender, EventArgs e)
        {
            if (Validation())
            {
                if (_enumFormStatus == EnumFormStatus.Modify)
                {
                    customer cus = addCustomer();
                    if (_customerPresenter.Update(cus, cus.Cus_Username) > 0)
                    {
                        ClsMsgBox.ThongTin("Cập nhật khách hàng thành công");
                        if (ClsPublic.SYSTEM_WRITELOG == "1")
                        {
                            SysLogPresenter log = new SysLogPresenter();
                            log.Add(_fromCode, EnumFormStatus.Modify, cus.Cus_Username, "");
                        }
                    }
                }
            }
        }

        private void buttonsArray1_btnEventClose_click(object sender, EventArgs e)
        {
            this.Hide();
        }
        #endregion

        private void lookUpCusType_EditValueChanged(object sender, EventArgs e)
        {
            if (_enumFormStatus == EnumFormStatus.Add)
            {
                customer_type cusType = _customerTypePresenter.GetById(lookUpCusType.EditValue);
                if (cusType!=null)
                {
                    txtPrefix.EditValue = cusType.CusT_Code;
                    sorted = _customerPresenter.GetMaxSortedByCusType(cusType.CusT_ID);
                    txtUsername.EditValue = sorted.ToString().PadLeft(5, '0');
                }
               
            }
        }




    }
}